<template>
  <div class="app-container">
    <el-row type="flex" class="warp-breadcrum">
      <el-col :span="24">
        <el-col :span="3" :offset="21">
          <div class="grid-content">
            <el-button
              type="primary"
              @click="jumpAddTrigger()"
            ><i class="el-icon-plus el-icon--right" />添加触发器
            </el-button>
          </div>
        </el-col>
      </el-col>
    </el-row>
    <el-table
      :v-loading="listLoading"
      :data="dataList"
      stripe
      border
      tooltip-effect="dark"
      style="width: 100%;margin-top:10px"
      @sort-change="sortChange"
    >
      <el-table-column
        :index="indexMethod"
        prop="id"
        label="序号"
        type="index"
        width="80"
        align="center"
      />
      <el-table-column
        label="触发器名称"
        sortable="custom"
        prop="trigger_name"
        width="320"
      />
      <el-table-column label="表达式" prop="triggers" width="320">
        <template slot-scope="prop">
          <div>
            <el-link type="primary" @click="jumpChangeItem(prop.row.id)">{{
              prop.row.expression
            }}</el-link>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="严重等级" prop="level" width="120" sortable="custom">
        <template slot-scope="prop">
          <el-tag v-if="prop.row.level === 0" type="info">信息</el-tag>
          <el-tag v-if="prop.row.level === 1" type="warning">警告</el-tag>
          <el-tag v-if="prop.row.level === 2" type="danger">严重警告</el-tag>
          <el-tag v-if="prop.row.level === 3" type="danger" size="medium">灾难警告</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status" width="80">
        <template slot-scope="prop">
          <el-switch
            v-model="prop.row.status"
            active-value="0"
            inactive-value="1"
            @change="changeTriggerStatus(prop.row.id, $event)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="prop">
          <el-button
            size="small"
            type="primary"
            @click="jumpChangeTrigger(prop.row.id)"
          >编辑</el-button
          >
          <el-button size="small" type="danger" @click="deleteTrigger(prop.row.id, prop.$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-col :span="24" class="toolbar block">
      <!--数据分页
     layout：分页显示的样式
     :page-size：每页显示的条数
     :total：总数
     具体功能查看地址：http://element-cn.eleme.io/#/zh-CN/component/pagination
     -->
      <el-pagination
        :page-sizes="[5, 10, 15]"
        :page-size="10"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-col>
  </div>
</template>

<script>
import { change_trigger_status, delete_trigger, trigger_list } from '../../api/trigger'
export default {
  name: 'TriggerList',
  data() {
    return {
      listLoading: true,
      // 列表数据
      dataList: [],
      // 列表前端分页
      pageList: {
        totalCount: '',
        pageSize: '',
        totalPage: '',
        currPage: ''
      },
      // 列表分页辅助类(传参)
      pageHelp: {
        // 和后端参数一样
        page: 1,
        // 后端参数为size
        size: 10,
        order: 'asc'
      },
      sortHelp: {
        prop: '',
        order: ''
      },
      total: 0,
      pageNum: 1
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      this.pageHelp.page = this.pageNum
      if (this.$route.query.template_id == null && this.$route.query.host_id != null) {
        trigger_list(Object.assign(this.pageHelp, this.sortHelp, { host_id: this.$route.query.host_id })).then(
          response => {
            this.dataList = response.data.items
            this.pageList = response.data.page
            this.listLoading = false
            this.total = response.data.count
          }
        )
      } else if (this.$route.query.template_id != null) {
        trigger_list(Object.assign(this.pageHelp, this.sortHelp, { template_id: this.$route.query.template_id })).then(
          response => {
            this.dataList = response.data.items
            this.pageList = response.data.page
            this.listLoading = false
            this.total = response.data.count
          }
        )
      } else if (this.$route.query.item_id != null) {
        trigger_list(Object.assign(this.pageHelp, this.sortHelp, { item_id: this.$route.query.item_id })).then(
          response => {
            this.dataList = response.data.items
            this.pageList = response.data.page
            this.listLoading = false
            this.total = response.data.count
          }
        )
      }
    },
    indexMethod(index) {
      return (this.pageList.currPage - 1) * this.pageList.pageSize + index + 1
    },
    handleSizeChange(val) {
      this.pageList.pageSize = val
      this.pageHelp.size = this.pageList.pageSize
      this.pageHelp.page = this.pageList.currPage
      this.fetchData()
    },
    // 点击分页sort-change
    handleCurrentChange(val) {
      this.pageNum = val
      this.fetchData()
    },
    sortChange(column, prop, order) {
      this.sortHelp.order = column.order
      this.sortHelp.prop = column.prop
      this.fetchData()
    },
    // 跳转到添加触发器页面
    jumpAddTrigger() {
      // 存在host_id，就是修改单个服务器上的触发器
      if (this.$route.query.host_id != null) {
        this.$router.push({ path: '/add_trigger?host_id=' + this.$route.query.host_id })
      } else if (this.$route.query.template_id != null) {
        this.$router.push({ path: '/add_trigger?template_id=' + this.$route.query.template_id })
      }
    },
    // 跳转到修改除法器页面
    jumpChangeTrigger(id) {
      // 存在host_id，就是修改单个服务器上的触发器
      if (this.$route.query.host_id != null) {
        this.$router.push({
          path: '/change_trigger',
          query: {
            id: id,
            host_id: this.$route.query.host_id
          }
        })
      } else if (this.$route.query.template_id != null) {
        this.$router.push({
          path: '/change_trigger',
          query: {
            id: id,
            template_id: this.$route.query.template_id
          }
        })
      } else {
        this.$router.push({
          path: '/change_trigger',
          query: { id: id }
        })
      }
    },
    // 修改监控项状态
    changeTriggerStatus(a, b) {
      change_trigger_status(a, b)
    },
    // 删除触发器
    deleteTrigger(id, rowIdx) {
      delete_trigger({ id: id }).then(response => {
        this.deleteRow(rowIdx, this.dataList)
      })
    },
    // 删除当前行
    deleteRow(index, rows) {
      rows.splice(index, 1)
    }
  }
}
</script>

<style scoped></style>
